home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / nroff / io.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  4KB  |  273 lines

  1. /*
  2.  *    io.c - low level I/O processing portion of nroff word processor
  3.  *
  4.  *    adapted for atariST/TOS by Bill Rosenkranz 11/89
  5.  *    net:    rosenkra@hall.cray.com
  6.  *    CIS:    71460,17
  7.  *    GENIE:    W.ROSENKRANZ
  8.  *
  9.  *    original author:
  10.  *
  11.  *    Stephen L. Browning
  12.  *    5723 North Parker Avenue
  13.  *    Indianapolis, Indiana 46220
  14.  *
  15.  *    history:
  16.  *
  17.  *    - Originally written in BDS C;
  18.  *    - Adapted for standard C by W. N. Paul
  19.  *    - Heavily hacked up to conform to "real" nroff by Bill Rosenkranz
  20.  */
  21.  
  22. #undef NRO_MAIN                    /* extern globals */
  23.  
  24. #include <stdio.h>
  25. #include "nroff.h"
  26.  
  27. /*------------------------------*/
  28. /*    getlin            */
  29. /*------------------------------*/
  30. getlin (p, in_buf)
  31. char   *p;
  32. FILE   *in_buf;
  33. {
  34.  
  35. /*
  36.  *    retrieve one line of input text
  37.  */
  38.  
  39.     register char  *q;
  40.     register int    i;
  41.     int        c;
  42.     int        nreg;
  43.  
  44.     q = p;
  45.     for (i = 0; i < MAXLINE - 1; ++i)
  46.     {
  47.         c = ngetc (in_buf);
  48.         if (c == EOF)
  49.         {
  50.             *q = EOS;
  51.             c  = strlen (p);
  52.             return (c == 0 ? EOF : c);
  53.         }
  54.         *q++ = c;
  55.         if (c == '\n')
  56.             break;
  57.     }
  58.     *q = EOS;
  59.  
  60.     nreg = findreg (".c");
  61.     if (nreg > 0)
  62.         set_ireg (".c", rg[nreg].rval + 1, 0);
  63.  
  64.     return (strlen (p));
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. /*------------------------------*/
  73. /*    ngetc            */
  74. /*------------------------------*/
  75. ngetc (infp)
  76. FILE   *infp;
  77. {
  78.  
  79. /*
  80.  *    get character from input file or push back buffer
  81.  */
  82.  
  83.     register int    c;
  84.  
  85.     if (mac.ppb >= &mac.pbb[0])
  86.         c = *mac.ppb--;
  87.     else
  88.         c = getc (infp);
  89.  
  90.     return (c);
  91. }
  92.  
  93.  
  94.  
  95. /*------------------------------*/
  96. /*    pbstr            */
  97. /*------------------------------*/
  98. pbstr (p)
  99. char   *p;
  100. {
  101.  
  102. /*
  103.  *    Push back string into input stream
  104.  */
  105.  
  106.     register int    i;
  107.  
  108.     /*
  109.      *   if string is null, we do nothing
  110.      */
  111.     if (p == NULL_CPTR)
  112.         return;
  113.     if (p[0] == EOS)
  114.         return;
  115.     for (i = strlen (p) - 1; i >= 0; --i)
  116.     {
  117.         putbak (p[i]);
  118.     }
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125. /*------------------------------*/
  126. /*    putbak            */
  127. /*------------------------------*/
  128. putbak (c)
  129. char    c;
  130. {
  131.  
  132. /*
  133.  *    Push character back into input stream. we use the push-back buffer
  134.  *    stored with macros.
  135.  */
  136.  
  137.     if (mac.ppb < &(mac.pbb[0]))
  138.     {
  139.         mac.ppb = &(mac.pbb[0]);
  140.         *mac.ppb = c;
  141.     }
  142.     else
  143.     {
  144.         if (mac.ppb >= &mac.pbb[MAXLINE - 1])
  145.         {
  146.             fprintf (err_stream,
  147.                 "***%s: push back buffer overflow\n", myname);
  148.             err_exit (-1);
  149.         }
  150.         *++(mac.ppb) = c;
  151.     }
  152. }
  153.  
  154.  
  155.  
  156. /*------------------------------*/
  157. /*    prchar            */
  158. /*------------------------------*/
  159. prchar (c, fp)
  160. char    c;
  161. FILE   *fp;
  162. {
  163.  
  164. /*
  165.  *    print character with test for printer
  166.  */
  167.  
  168.     if (fp == stdout)
  169.         putc (c, fp);
  170.     else
  171.         putc_lpr (c, fp);
  172. }
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. /*------------------------------*/
  180. /*    put            */
  181. /*------------------------------*/
  182. put (p)
  183. char   *p;
  184. {
  185.  
  186. /*
  187.  *    put out line with proper spacing and indenting
  188.  */
  189.  
  190.     register int    j;
  191.     char        os[MAXLINE];
  192.  
  193.     if (pg.lineno == 0 || pg.lineno > pg.bottom)
  194.     {
  195.         phead ();
  196.     }
  197.     if (dc.prflg == TRUE)
  198.     {
  199.         if (!dc.bsflg)
  200.         {
  201.             if (strkovr (p, os) == TRUE)
  202.             {
  203.                 for (j = 0; j < pg.offset; ++j)
  204.                     prchar (' ', out_stream);
  205.                 for (j = 0; j < dc.tival; ++j)
  206.                     prchar (' ', out_stream);
  207.                 putlin (os, out_stream);
  208.             }
  209.         }
  210.         for (j = 0; j < pg.offset; ++j)
  211.             prchar (' ', out_stream);
  212.         for (j = 0; j < dc.tival; ++j)
  213.             prchar (' ', out_stream);
  214.         putlin (p, out_stream);
  215.     }
  216.     dc.tival = dc.inval;
  217.     skip (min (dc.lsval - 1, pg.bottom - pg.lineno));
  218.     pg.lineno = pg.lineno + dc.lsval;
  219.     set_ireg ("ln", pg.lineno, 0);
  220.     if (pg.lineno > pg.bottom)
  221.     {
  222.         pfoot ();
  223.         if (stepping)
  224.             wait_for_char ();
  225.     }
  226. }
  227.  
  228.  
  229.  
  230.  
  231. /*------------------------------*/
  232. /*    putlin            */
  233. /*------------------------------*/
  234. putlin (p, pbuf)
  235. register char  *p;
  236. FILE           *pbuf;
  237. {
  238.  
  239. /*
  240.  *    output a null terminated string to the file
  241.  *    specified by pbuf.
  242.  */
  243.  
  244.     while (*p != EOS)
  245.         prchar (*p++, pbuf);
  246. }
  247.  
  248.  
  249.  
  250.  
  251. /*------------------------------*/
  252. /*    putc_lpr        */
  253. /*------------------------------*/
  254. #ifdef GEMDOS
  255. #include <osbind.h>
  256. #endif
  257.  
  258. putc_lpr (c, fp)
  259. char    c;
  260. FILE   *fp;
  261. {
  262.  
  263. /*
  264.  *    write char to printer
  265.  */
  266.  
  267. #ifdef GEMDOS
  268.     Bconout (0, (int) c & 0x00FF);
  269. #else
  270.     putc (c, fp);
  271. #endif
  272. }
  273.